//
//  ByViewServiceForInHospitalDailyBill.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/12/11.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewServiceForInHospitalDailyBill.h"

@implementation ByViewServiceForInHospitalDailyBill
{
    UILabel *_titleLabel;
    CAShapeLayer *_chart;
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.text = @"日清单说明：";
    _titleLabel.font = FONT(16);
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleLabel.textColor = [UIColor blackColor];
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(@10);
    }];
    
    _chart = [CAShapeLayer layer];
    _chart.fillColor = [UIColor clearColor].CGColor;
    _chart.strokeColor = [UIColor grayColor].CGColor;
    _chart.lineWidth = 1;
    [self.layer addSublayer:_chart];
    
    NSArray *items = @[@{@"项目": @"金额"}, @{@"项目": @"金额"},
                       @{@"床位费": @""}, @{@"检验病理费": @""},
                       @{@"监护费": @""}, @{@"陪侍费": @""},
                       @{@"西药": @""}, @{@"放射费": @""},
                       @{@"监护费": @""}, @{@"采暖费": @""},
                       @{@"检查费": @""}, @{@"氧气费": @""},
                       @{@"血费": @""}, @{@"治疗费": @""},
                       @{@"接生费": @""}, @{@"诊疗费": @""},
                       ];
    
    __block UIView *lastViewAtH = nil;
    UIView *lastViewAtV = nil;
    CGFloat cellWidthOffset = -20 / 4.0;
    CGFloat cellHeight = 30;
    for (int i = 0; i < items.count; i ++) {
        NSDictionary *dic = items[i];
        NSString *title = dic.allKeys.firstObject;
        NSString *content = dic[title];
        
        UILabel *titlelabel = [[UILabel alloc] init];
        titlelabel.text = title;
        titlelabel.font = FONT(14);
        titlelabel.textAlignment = NSTextAlignmentCenter;
        titlelabel.textColor = [UIColor grayColor];
        [self addSubview:titlelabel];
        
        UILabel *contentLabel = [[UILabel alloc] init];
        contentLabel.text = content;
        contentLabel.font = FONT(14);
        contentLabel.textAlignment = NSTextAlignmentCenter;
        contentLabel.textColor = [UIColor grayColor];
        [self addSubview:contentLabel];
        
        [titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(cellHeight));
            make.width.equalTo(self).multipliedBy(1.0 / 4).offset(cellWidthOffset);
            if (lastViewAtH) {
                make.left.equalTo(lastViewAtH.mas_right);
            } else {
                make.left.equalTo(@10);
            }
            if (lastViewAtV) {
                make.top.equalTo(lastViewAtV.mas_bottom);
            } else {
                make.top.equalTo(_titleLabel.mas_bottom).offset(10);
            }
        }];
        [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(titlelabel);
            make.left.equalTo(titlelabel.mas_right);
            make.top.equalTo(titlelabel);
        }];
        
        if (i % 2 == 0) {
            lastViewAtH = contentLabel;
        } else {
            lastViewAtV = titlelabel;
            lastViewAtH = nil;
        }
    }
    
    [lastViewAtV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
    }];
}
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    CGFloat cellWidth = self.subviews.lastObject.width;
    CGFloat cellHeight = self.subviews.lastObject.height;
    for (int i = 0; i <= 4; i ++) {
        [path moveToPoint:CGPointMake(10 + i * cellWidth, _titleLabel.bottom + 10)];
        [path addLineToPoint:CGPointMake(10 + i * cellWidth, self.height)];
    }
    for (int i = 0; i <= 8; i ++) {
        [path moveToPoint:CGPointMake(10, _titleLabel.bottom + 10 + i * cellHeight)];
        [path addLineToPoint:CGPointMake(self.width - 10, _titleLabel.bottom + 10 + i * cellHeight)];
    }
    
    _chart.path = path.CGPath;
    
}
@end
